package com.navinfo.platform.trip.common.arithmetic.var;

import com.navinfo.platform.trip.common.arithmetic.data.InterRangeData;
import com.navinfo.platform.trip.common.util.ArithmeticUtils;
import lombok.Data;

import java.util.Map;
import java.util.TreeMap;

/**
 * 公共的区间段统计变量
 * @author web
 */
@Data
public class VarCommonRange {
    /**
     * 区间值索引，公共统计对象的Map
     */
    private final Map<Integer, VarCommon> dataMap = new TreeMap<>();

    /**
     * 获取指定的区间段统计的指标
     * @param index 区间值索引
     * @return 统计值
     */
    public VarCommon getRangeData(Integer index){
        VarCommon data = dataMap.get(index);
        if (data == null) {
            data = new VarCommon();
            dataMap.put(index, data);
        }
        return data;
    }

    /**
     * 获取区间段统计的内部对象
     * @return
     */
    public Map<Integer, InterRangeData> convertRanges() {
        Map<Integer, InterRangeData> ranges = new TreeMap<>();
        for (Map.Entry<Integer, VarCommon> entry : dataMap.entrySet()) {
            VarCommon varCommon = entry.getValue();
            InterRangeData interRangeData = new InterRangeData();
            interRangeData.setMileage(ArithmeticUtils.mileageMToM(varCommon.getMileage()));
            interRangeData.setFuel(ArithmeticUtils.fuelMlToMl(varCommon.getFuel()));
            interRangeData.setTime(ArithmeticUtils.timeMsToSecond(varCommon.getDuration()));
            ranges.put(entry.getKey(), interRangeData);
        }
        return ranges;
    }
}
